package com.ruoyi.material.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.material.domain.BaseLabel;
import com.ruoyi.material.domain.vo.LabelTypeVo;
import com.ruoyi.material.service.IBaseLabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.BaseLabelType;
import com.ruoyi.material.service.IBaseLabelTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标签类别Controller
 * 
 * @author ruoyi
 * @date 2021-07-27
 */
@RestController
@RequestMapping("/material/labelType")
public class BaseLabelTypeController extends BaseController
{
    private Logger logger = LoggerFactory.getLogger(BaseFlieRecordController.class);
    @Autowired
    private IBaseLabelTypeService baseLabelTypeService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBaseLabelService baseLabelService;
    /**
     * 查询标签类别列表
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseLabelType baseLabelType)
    {
        startPage();
        List<LabelTypeVo> list = baseLabelTypeService.selectBaseLabelTypeList(baseLabelType);
        return getDataTable(list);
    }

    /**
     * 导出标签类别列表
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:export')")
    @Log(title = "标签类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseLabelType baseLabelType)
    {
        List<LabelTypeVo> list = baseLabelTypeService.selectBaseLabelTypeList(baseLabelType);
        ExcelUtil<LabelTypeVo> util = new ExcelUtil<LabelTypeVo>(LabelTypeVo.class);
        return util.exportExcel(list, "标签类别数据");
    }

    /**
     * 获取标签类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:query')")
//    @GetMapping(value = "/{id}")
    @RequestMapping(method= RequestMethod.GET,value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseLabelTypeService.selectBaseLabelTypeById(id));
    }

    /**
     * 新增标签类别
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:add')")
    @Log(title = "标签类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseLabelType baseLabelType)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        baseLabelType.setCreateBy(loginUser != null ? loginUser.getUsername() : "anonymous");
        return toAjax(baseLabelTypeService.insertBaseLabelType(baseLabelType));
    }

    /**
     * 修改标签类别
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:edit')")
    @Log(title = "标签类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseLabelType baseLabelType)
    {
        return toAjax(baseLabelTypeService.updateBaseLabelType(baseLabelType));
    }

    /**
     * 删除标签类别
     */
    @PreAuthorize("@ss.hasPermi('material:labelType:remove')")
    @Log(title = "标签类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String updateBy=loginUser != null ? loginUser.getUsername() : "anonymous";
        return toAjax(baseLabelTypeService.deleteBaseLabelTypeByIds(ids,updateBy));
    }
    /**
     * 获取标签类别详细信息
     */
    @GetMapping("/typeList")
    public List<String> getAllType()
    {
        List<BaseLabelType> list=baseLabelTypeService.selectAllType();
        List<String> labelTypeList=new ArrayList<>();
        for(BaseLabelType b:list){
            labelTypeList.add(b.getLabelType());
        }
        return labelTypeList;
    }

    /**
     * 获取标签类别详细信息
     */
    @GetMapping("/findIdByLabelName/{labelType}")
    public int findIdByLabelName(@PathVariable("labelType") String labelType)
    {
        return baseLabelTypeService.selectBaseLabelTypeByLabel(labelType);
    }
    /**
     * 根据标签id获取标签类别名称
     */
    @GetMapping("/findLabelTypeNameByid/{id}")
    public String findLabelTypeNameByid(@PathVariable("id") Long id)
    {
        BaseLabel baseLabel=baseLabelService.selectBaseLabelById(id);
        BaseLabelType baseLabelType=baseLabelTypeService.selectBaseLabelTypeById(baseLabel.getTypeCode());
        return baseLabelType.getLabelType();
    }
}
